/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/

/**
 * \file
 * \brief Implementation of Parameter interface Server.
 * \author Antti Maula <antti.maula@tkk.fi>
 *
 * $Id: ParameterServer.cpp,v 1.1 2009-09-22 08:52:14 morsko Exp $
 *
 */
#include "ParameterServer.hpp"
#include "ParameterData.hpp"

//*****************************************************************************
#define GIMI_CLIENT_API (20000)
#include "gimi.h"
//*****************************************************************************
//*****************************************************************************
using namespace MaCI::Parameter;
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************

CParameterServer::CParameterServer(MaCICtrl::CMaCICtrlServer *aMaCICtrlServerPtr, 
                           const int aInterfaceMinor,
                           const std::string &aDescription,
                           const std::string &aInstanceName)
  : CMaCI(aMaCICtrlServerPtr, GIMI_PROTOCOL_MACI_PARAMETER, aInterfaceMinor, aInstanceName),
    iServerActive(false),
    iServiceDescription(aDescription)
    
{
}
//*****************************************************************************

CParameterServer::~CParameterServer()
{
  DoClose();
}
//*****************************************************************************

MaCI::EMaCIError CParameterServer::DoOpen(void)
{
  EMaCIError result = KMaCIError;
  assert(this != NULL);
  assert(iGIMIPtr != NULL);
  
  // Execute only if server is not active.
  if (!iServerActive) {
    // Register service.
    RegisterProvidedService("MaCI-ParameterServer; '" + iServiceDescription + "'");
    RegisterAcceptedService("MaCI-ParameterServer-Cmd; '" + iServiceDescription + "'");
    
    // Start the handler Thread
    iServerActive = true;
    //    CThread::RunThread();
    
    result = KMaCIOK;
    
    dPrint(4,"Succesfully Open():ed ParameterServer instance %p.", this);
  }
    
  return result;
}
//*****************************************************************************

MaCI::EMaCIError CParameterServer::DoClose(void)
{
  EMaCIError result = KMaCIError;
  assert(this != NULL);
  assert(iGIMIPtr != NULL);

  // Execute block only if server is currently active.
  if (iServerActive) {
    // Stop!
    iServerActive = false;

    // Stop the handler Thread
    //    /* CThread::CancelThread(); //Removed by script */
    //    CThread::WaitThread();    

    // Unregister services
    UnregisterProvidedService();
    UnregisterAcceptedService();
    
    dPrint(4,"Succesfully Close():ed ParameterServer instance %p", this);
  }
  
  return result;
}
//*****************************************************************************

MaCI::EMaCIError CParameterServer::WaitCommand(CParameterData &aData, 
                                         SMessageSource &aSource,
                                         const unsigned int aTimeout_ms)
{
  // Call ReceiveInterfaceMessage
  EMaCIError e = ReceiveInterfaceMessage(aData, aSource, aTimeout_ms);

  // Check result
  if (e == KMaCIOK) {
    // If OK is returned, a message was received and succesfully
    // converted to type CParameterData. Basicly we don't need to do
    // anything here, but some interface may want to do some checks:
    // for example, the received message contains some specific
    // element or information before it is accepted.
  }

  return e;
}
//*****************************************************************************

MaCI::EMaCIError CParameterServer::SendReply(const CParameterData &aData,
                                       const SMessageSource &aSource)
{
  EMaCIError result = KMaCIError;
  // Check that server is active.
  if (iServerActive) {
    // Send
    result = SendSynchronousMessageReply(aData.GetBinBagContainerPtr()->GetBuffer(),
                                         aData.GetBinBagContainerPtr()->GetBufferSize(),
                                         aSource);
    
  } else {
    dPrint(2,"Unable to send ParameterData - Interface is not active! (Open() not called?)");
    result = KMaCINotActive;

  }

  return result;
}
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
